import service
import blue
import const
import geo2
import GameWorld
import svc
import zaction

class ActionObjectClientSvc(svc.actionObjectSvc):
    __guid__ = 'svc.actionObjectClientSvc'
    __displayname__ = 'ActionObject Service'
    __machoresolve__ = 'location'
    __notifyevents__ = ['ProcessSessionChange']

    def __init__(self):
        svc.actionObjectSvc.__init__(self)
        GameWorld.RegisterPythonActionProc('PerformPythonUICallback', self._PerformUICallback, ('callbackKey',))
        GameWorld.RegisterPythonActionProc('PlayEntityAudio', self._PlayEntityAudio, ('audioName', 'mls', 'TargetList'))



    def Run(self, *args):
        self.actionObjectServer = None
        svc.actionObjectSvc.Run(self, *args)
        self.zactionClient = sm.GetService('zactionClient')



    def _PerformUICallback(self, callbackKey):
        raise NotImplementedError('Each game must implement a _PerformUICallback that works with its UI.')



    def _PlayEntityAudio(self, audioName, mls, targetList):
        if mls:
            message = cfg.GetMessage(audioName)
            audioName = message.audio
            if audioName.startswith('wise:/'):
                audioName = audioName[6:]
        for entityID in targetList:
            entity = self.entityService.FindEntityByID(entityID)
            audioComponent = entity.GetComponent('audioEmitter')
            if audioComponent:
                audioComponent.emitter.SendEvent(unicode(audioName))
            else:
                self.LogWarn(('Entity with ID %s has no audio component. Audio file %s cannot be played from this entity.' % (entityID, audioName)))




    def ProcessSessionChange(self, isRemote, session, change):
        if ('charid' in change):
            if (self.actionObjectServer is None):
                self.actionObjectServer = sm.RemoteSvc('actionObjectServerSvc')



    def GetActionList(self, entID, objectID):
        returnDict = {}
        actionList = self.manager.GetAllAvailableActions(objectID)
        if ((actionList is not None) and (len(actionList) > 0)):
            tree = self.zactionClient.GetActionTree()
            entity = self.entityService.FindEntityByID(entID)
            treeInst = entity.GetComponent('action').treeInstance
            for action in actionList:
                returnDict[action] = []
                returnDict[action].append(tree.GetTreeNodeByID(action).name)
                returnDict[action].append(treeInst.IsActionAvailable(action, {'TargetList': [objectID]}))

        return returnDict



    def TriggerActionOnObject(self, entID, targetID, actionID):
        self.zactionClient.StartAction(entID, actionID, {'TargetList': [targetID]})



exports = {'actionProcTypes.PerformPythonUICallback': zaction.ProcTypeDef(isMaster=True, procCategory='UI', properties=[zaction.ProcPropertyTypeDef('callbackKey', 'S', userDataType=None, isPrivate=True)]),
 'actionProcTypes.PlayEntityAudio': zaction.ProcTypeDef(isMaster=True, procCategory='Audio', properties=[zaction.ProcPropertyTypeDef('audioName', 'S', userDataType=None, isPrivate=True, displayName='Audio Name'), zaction.ProcPropertyTypeDef('mls', 'B', userDataType=None, isPrivate=True, displayName='MLS')])}

